package com.whiteseason.whitebot.plugin;

import com.whiteseason.whitebot.dao.UserRepository;
import com.whiteseason.whitebot.pojo.User;
import com.whiteseason.whitebot.pojo.UserInfo;
import com.whiteseason.whitebot.util.OSU.UserGet;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;
import lombok.extern.slf4j.Slf4j;
import net.lz1998.cq.event.message.CQGroupMessageEvent;
import net.lz1998.cq.event.message.CQPrivateMessageEvent;
import net.lz1998.cq.robot.CQPlugin;
import net.lz1998.cq.robot.CoolQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.client.RestClientException;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class QueryPlugin extends CQPlugin {
  @Autowired
  UserRepository userRepository;
  @Autowired
  UserGet userGet;
  @Autowired
  FreeMarkerConfigurer configurer;
  Map<String, Object> printUserInfo = null;
  @Override
  public int onPrivateMessage(CoolQ cq, CQPrivateMessageEvent event) {
    String msg = event.getMessage();

    if(msg.startsWith("mystd") || msg.startsWith("std")){
      User byQq = null;
      UserInfo user = null;
      //mystd查询处理
      if(msg.startsWith("mystd")) {
        if(msg.length() != 5){
          return MESSAGE_IGNORE;
        }
        byQq = userRepository.findByQq(Long.toString(event.getUserId()));
        if (byQq == null) {
          cq.sendPrivateMsg(event.getUserId(), "此qq尚未绑定，请使用!setname绑定", false);
          return MESSAGE_BLOCK;
        }else {
          user = userGet.getUser(byQq.getUserId(), 0);
        }
      }
      //std查询处理
      else{
        if(!msg.substring(3).startsWith(" ")){
          return MESSAGE_IGNORE;
        }
        user = userGet.getUser(msg.split(" ",2)[1],0);
      }
        try {
          printUserInfo = new HashMap<>();
          printUserInfo.put("newuserinfo", user);
          printUserInfo.put("olduserinfo", byQq);
          Template template = configurer.getConfiguration().getTemplate("UserInfo.ftl");
          String templatemsg = FreeMarkerTemplateUtils.
              processTemplateIntoString(template, printUserInfo);
          cq.sendPrivateMsg(event.getUserId(),templatemsg,false);
        }catch (RestClientException rce){
          cq.sendPrivateMsg(event.getUserId(),"网络异常，请重试或联系白季",false);
          log.error("查询网络异常",rce);
          return MESSAGE_BLOCK;
        } catch (TemplateNotFoundException e) {
          cq.sendPrivateMsg(event.getUserId(),"模板失效，请联系白季解决",false);
          log.error("模板找不到，检查模板文件",e);
          return MESSAGE_BLOCK;
        } catch (IOException e) {
          cq.sendPrivateMsg(event.getUserId(),"模板IO异常，请联系白季解决",false);
          log.error("模板IO异常，检查模板配置",e);
          return MESSAGE_BLOCK;
        } catch (TemplateException e) {
          cq.sendPrivateMsg(event.getUserId(),"模板生成异常，请联系白季解决",false);
          log.error("模板生成异常，请检查templateutil类",e);
          return MESSAGE_BLOCK;
        }

    }
    return MESSAGE_IGNORE;
  }

  @Override
  public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event) {
    String msg = event.getMessage();

    if(msg.startsWith("mystd") || msg.startsWith("std")){
      User byQq = null;
      UserInfo user = null;
      //mystd查询处理
      if(msg.startsWith("mystd")) {
        if(msg.length() != 5){
          return MESSAGE_IGNORE;
        }
        byQq = userRepository.findByQq(Long.toString(event.getUserId()));
        if (byQq == null) {
          cq.sendGroupMsg(event.getGroupId(), "此qq尚未绑定，请使用!setname绑定", false);
          return MESSAGE_BLOCK;
        }else {
          user = userGet.getUser(byQq.getUserId(), 0);
        }
      }
      //std查询处理
      else{
        if(!msg.substring(3).startsWith(" ")){
          return MESSAGE_IGNORE;
        }
        user = userGet.getUser(msg.split(" ",2)[1],0);
      }
      try {
        printUserInfo = new HashMap<>();
        printUserInfo.put("newuserinfo", user);
        printUserInfo.put("olduserinfo", byQq);
        Template template = configurer.getConfiguration().getTemplate("UserInfo.ftl");
        String templatemsg = FreeMarkerTemplateUtils.
                processTemplateIntoString(template, printUserInfo);
        cq.sendGroupMsg(event.getGroupId(),templatemsg,false);
      }catch (RestClientException rce){
        cq.sendGroupMsg(event.getGroupId(),"网络异常，请重试或联系白季",false);
        log.error("查询网络异常",rce);
        return MESSAGE_BLOCK;
      } catch (TemplateNotFoundException e) {
        cq.sendGroupMsg(event.getGroupId(),"模板失效，请联系白季解决",false);
        log.error("模板找不到，检查模板文件",e);
        return MESSAGE_BLOCK;
      } catch (IOException e) {
        cq.sendGroupMsg(event.getGroupId(),"模板IO异常，请联系白季解决",false);
        log.error("模板IO异常，检查模板配置",e);
        return MESSAGE_BLOCK;
      } catch (TemplateException e) {
        cq.sendGroupMsg(event.getGroupId(),"模板生成异常，请联系白季解决",false);
        log.error("模板生成异常，请检查templateutil类",e);
        return MESSAGE_BLOCK;
      }

    }
    return MESSAGE_IGNORE;
  }

}
